package org.snail.quartzController;

import lombok.extern.log4j.Log4j;
import org.snail.chat.service.chatchain.quartz.DingGroupQuartzJob;
import org.snail.chat.service.impl.ChatServiceImpl;
import org.snail.constant.QuartzConstant;
import org.snail.exception.JsonResult;
import org.snail.quartz.QuartzSchedulingUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("quartz")
@Log4j
public class QuartzController {

    @Autowired
    ChatServiceImpl chatService;

    @RequestMapping("dingGroupQuartz")
    @ResponseBody
    public JsonResult initDingGroupQuartz() {
        JsonResult jsonResult = new JsonResult();
        try {
            QuartzSchedulingUtils.addJob(QuartzConstant.JOB_NAME, QuartzConstant.JOB_GROUP_NAME, QuartzConstant.TRIGGER_NAME,
                    QuartzConstant.TRIGGER_GROUP_NAME, DingGroupQuartzJob.class, QuartzConstant.TRRIGGER_TIME);
            jsonResult.setCode(200);
            jsonResult.setMessage("钉钉群组定时器启动成功");
        } catch (Exception e) {
           log.error("addJob fail", e);
            jsonResult.setCode(200);
            jsonResult.setMessage("钉钉群组定时器启动失败");
        }


        return jsonResult;
    }


    @RequestMapping("test")
    @ResponseBody
    public JsonResult test(){
        JsonResult  jsonResult = new JsonResult();
        try{
            chatService.sychronizeUserDepatChat();
        }catch (Exception e){
            log.error("同步失败,错误消息为5:"+e.getMessage());
        }

        return jsonResult;
    }

}
